<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/sinopac-fn" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${fn:url('/form/CSS/tabs.css')}">
<link rel="stylesheet" href="${fn:url('/form/CSS/jquery-ui-1.10.1.css')}">
<script type="text/javascript" src="${fn:url('/js/jQuery/jquery-1.8.2.min.js')}"></script>
<script type="text/javascript" src="${fn:url('/js/jQuery/jquery_DOMWindow.js')}"></script>
<script type="text/javascript" src="${fn:url('/js/jQuery/jquery-ui-autocomplete.min.js')}"></script>
<script type="text/javascript" src="${fn:url('/js/tools/formUtil.js')}"></script>
<script>
	$(function() {
    	$("input[name=upmd]").click(function(){
    		var custContact = $('#custContact').val();
    		var custPhone = $('#custPhone').val();
    		var custAddress = $('#custAddress').val();
    		var mailArea = $('#mailArea').val();
    		var mailBarCode = $('#mailBarCode').val();
    		var data = {"custContact":custContact,"custPhone":custPhone,"custAddress":custAddress,"mailArea":mailArea,"mailBarCode":mailBarCode};
    		var url = "${fn:url('/ccroger/updateCust')}";
    		$.post(url,data,function(data){
    			//alert(data);
    		})
    		.success(function() { alert("客戶資料更新完成！"); })
    		.error(function() { alert("客戶資料更新失敗！ 請檢查客戶資料內容是否正確"); })
    	});

    	//if(<s:property value="vo.token_esn!=null"/> || <s:property value="vo.token_status!=0"/>){
    	if(<s:property value="vo.token_apply!=0"/>){
    		 $(".off").css("display","none");
    		 $('.open').css("display","");
   			 alert($('#message').val());
    		 //var f = $('#form1');
	 		 //f.prop('action',"${fn:url('/Index/backMain')}");
	
	 		 //f.submit();
    		 //$(".upmd").css("display","none");
    		
		}

    	if($('#errorMessage_a').val() !=""){
			alert($('#errorMessage_a').val());		
		}
    	
    	$("#token_esn").autocomplete({
    		delay: 1000,
    		minLength: 1,
            source: function(request, response) {
                $.ajax({
                    url: "${fn:url('/ccroger/queryToken')}",
                    dataType: "json",
                    type: "post",
                    data: {"search":request.term},
                    success: function(data) {
				    	var dataset = [];
                    	var jo = eval("("+data+")");   
                        $(jo).each(function(i,item){ 
                        	dataset.push(item.tokenEsn);
                        });  
                        response(dataset);
                        $('.ui-menu-item').css("font-size","12px");
                        $('.ui-menu-item').css("font-weight","normal");
                    },
                });
            }
        });
    	
    	$('input[name=btnPrint]').click(function(){
    		if($('#token_esn').val()==""){
    			alert("TOKEN ESN 必須輸入!");
    			$('#token_esn').focus();
    			return false;
    		}
    		var url = "${fn:url('/ccroger/chkToken')}";
    		var token_esn = $('#token_esn').val();
    		$.post(url,{"token_esn":token_esn},function(callback){
    			if(callback!=""){
    				alert(callback);
    			}else{
    				var f = $('#form1');
    				f.prop("action","${fn:url('/CustomerApply/printRoport')}");
    				f.submit();
    			}
    		});
    	});
	});

	//新增btnClick 
	function getNewRelateCustId(){
		if(($('#newCustId01_a').val() == "" || $('#newCustId02_a').val() == "" || $('#newCustId03_a').val() == "")){
			alert("客戶ID不可為空白!!!");
		}else{
			var str = "";
			str = $.joinId('#newCustId01_a', '#newCustId02_a', '#newCustId03_a');
			$('#nrc').val(str);
			
			var f = $('#form1');
			f.prop('action',"${fn:url('/ccroger/addNew')}");

			f.submit();
		}		
	}

	//關聯客戶表格  設定btnClick 
	var j_index_u = 0;
	function updateData(j_index_u,relateCustId){
		var u = $('#form1');
		u.prop('action','/sinopac/ccroger/update.action');
		$('input[name=relateCustId]').val(relateCustId);
		u.submit();
	}
	
	function removeRelated(custId){
		var u = $('#form1');
		u.prop('action','/sinopac/ccroger/removeRelated');
		$('input[name=relateCustId]').val(custId);
		u.submit();
	}
</script>
<title>快速配發</title>
</head>
<body>
	<%@ include file="/form/include/title.jsp" %>
	<s:form id="form1">
		<input type="hidden" name="relateCustId" />		
		<input type="hidden" name="custId"/>
		<input type="hidden" value='<s:text name="token_apply_%{vo.token_apply}"/>' id="message"/>
		<s:hidden id="errorMessage_a" name="errorMessage"/>
		<table class="table-style" align="center">
		<tr>
			<th>客戶ID</th>
			<th>客戶名稱</th>
			<th>AO Code</th>
			<th>OTP 狀態</th>
			<th nowrap="nowrap">TOKEN 配發日</th>
			<th nowrap="nowrap">最近異動經辦</th>
		</tr>
		<tr>
			<td><s:property value="vo.customerId"/></td>
			<td><s:property value="vo.customerName"/></td>
			<td><s:property value="vo.AOCode"/></td>
			<td align="center"><s:text name="otp_status_%{vo.otpStatus}"/></td>
			<td><s:property value="vo.related_to_time"/></td>
			<td><s:property value="vo.operator"/></td>
		</tr>
		</table>
		<table class="table-style" align="center">
			<tr height="30"/>
			<tr>
				<th>收件人</th>
				<th>聯絡人電話</th>
				<th>聯絡地址</th>
			</tr>
			<tr class="off">
				<td><input type="text" size="25" id="custContact" name="vo.customerContact" value="${ vo.customerContact }"></td>
				<td><input type="text" size="25" id="custPhone" name="vo.customerPhone" value="${ vo.customerPhone }"/></td>
				<td><textarea cols="60" rows="2" id="custAddress" name="vo.customerAddress">${ vo.customerAddress }</textarea></td>
			</tr>
			<tr class="open" style="display: none;">
				<td><s:property value="vo.customerContact"/>&nbsp;</td>
				<td><s:property value="vo.customerPhone"/>&nbsp;</td>
				<td nowrap="nowrap"><s:property value="vo.customerAddress"/>&nbsp;</td>
			</tr>
			<tr>
				<th>郵件區域</th>
				<th>郵件條碼</th>
			</tr>
			<tr class="off">
				<td><input type="text" size="25" id="mailArea" name="vo.mailArea" value="${ vo.mailArea }"></td>
				<td><input type="text" size="25" id="mailBarCode" name="vo.mailBarCode" value="${ vo.mailBarCode }"/></td>
			</tr>
			<tr class="open" style="display: none;">
				<td><s:property value="vo.mailArea"/></td>
				<td><s:property value="vo.mailBarCode"/></td>
			</tr>

			<tr height="10">
		</table>
		<table class="upmd" align="center">
			<tr>
			<!-- 
				<th>郵件區號</th>
				<th>郵件號碼</th>
			 -->
				<td rowspan="2" style="background-color: #FFFFFF; width: 150px;" align="center">
					<input type="button" name="upmd" value="<s:text name='btn_upmd'/>" class="off">
				</td>
			</tr>
			<tr>
			<!-- 
				<td><input type="text" size="25" value="0" style="font-weight: bold;"></td>
				<td><input type="text" size="25" style="font-weight: bold;"></td>
			 -->
			</tr>
		</table>
		<div align="center" style="display: none;"><b><s:text name="message_ccroger1"/></b></div>
		<br>
		<br>
		<table align="center" class="off">
			<tr height="80">
				<td>請輸入客戶ID<br>新增至關聯:</td>
				<td><input type="text" id="newCustId01_a" size="18" maxlength="18"></td>
				<td>-</td>
				<td><input type="text" id="newCustId02_a" size="1" maxlength="1"></td>
				<td>-</td>
				<td><input type="text" id="newCustId03_a" size="2" maxlength="2"></td>
				<td><s:hidden name="newRelateCustId" id="nrc"></s:hidden></td>
				<td style="width: 100px" align="center">
					<input type="button" onclick="getNewRelateCustId()" value="<s:text name='btn_new'/>">
				</td>
			</tr>
		</table>
		<table class="table-style" align="center">
			<tr>
				<th>客戶ID</th>
				<th>客戶名稱</th>
				<th>AO Code</th>
				<th>OTP狀態</th>
				<th>主帳戶</th>
				<th>移除</th>
			</tr>
			<s:iterator value="vo.groupList" var="group" status="row">
				<tr>
					<td style="display: none;"><s:property value="#row.index"/></td>
					<td><s:property value="customerId"/></td>
					<td><s:property value="customerName"/></td>
					<td><s:property value="AOCode"/></td>
					<td align="center"><s:text name="otp_status_%{otpStatus}"/></td>
					<!-- 是否為主帳戶轉換　start -->
					<s:if test="isGroupMain == '0'.toString()">
						<s:if test='%{userRoles == "2"}'>
							<td align="center">否</td>
						</s:if>
						<s:else>
							<td align="center"><a name="isGroupMain" onclick="updateData(${row.index},'${group.customerId}')" id="setMainAcct_a" href="#" class="off"><s:text name="btn_setting"/></a></td>
						</s:else>							
					</s:if>
					<s:else>
						<td align="center">是</td>
					</s:else>
					<td align="center"><a href="#" onclick="removeRelated('${group.customerId}')" class="off"><s:text name="btn_remove"/></a></td>
				</tr>
			</s:iterator>
			<tr height="50">
		</table>
		<table class="table-style off" align="center">
				<tr>
				<th>客戶ID</th>
				<th>TOKEN ESN</th>
				<!-- 
				<td rowspan="2" style="background-color: #FFFFFF; width: 150px;" align="center">
					<input type="button" value="<s:text name='btn_ok'/>">
				</td>
				 -->
			</tr>
			<tr>
				<td width="200"><s:property value="vo.customerId"/></td>
				<td><input type="text" size="25" maxlength="16" value="${ vo.token_esn }" id="token_esn"></td>
			</tr>
			<tr height="15">
			<tr>
				<td colspan="3"  align="center" style="background-color: #FFFFFF;">
					<input type="button" name="btnPrint" value="<s:text name='btn_okAndPrint'/>">
					<!-- 
					<input type="button" name="btnCancel" value="<s:text name='btn_cancel'/>">
					 -->
				</td>
			</tr>
			<tr height="50">
		</table>
	</s:form>
</body>
</html>